package com.gitee.huanminabc.fastboot.load.metainfo;

import com.gitee.huanminabc.jcommon.file.IoStreamUtil;
import com.gitee.huanminabc.jcommon.reflect.MetaInfScanner;
import com.gitee.huanminabc.jcommon.str.StringUtil;
import com.gitee.huanminabc.jcommon.test.PathUtil;
import com.gitee.huanminabc.fastboot.context.AppContext;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.util.List;

/**
 * 简要描述
 *
 * @Author: huanmin
 * @Date: 2025/4/12 22:42
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
@Slf4j
public class ScanMetaInfoModule {
    public final static String fileName = "fastboot.module";

    public static void load() {

        try {
            List<InputStream> filesInMetaInf = MetaInfScanner.readFilesInMetaInf(fileName);
            if (filesInMetaInf.isEmpty()){
                log.debug("没有找到 {} 文件", fileName);
                return;
            }
            for (InputStream inputStream : filesInMetaInf) {
                try {
                    String str = IoStreamUtil.inputStreamToStr(inputStream);
                    //按照行读取
                    String[] lines = str.split("\n");
                    for (String line : lines) {
                        line = line.trim();
                        if (StringUtil.isEmpty(line)){
                            continue;
                        }
                        log.debug(fileName+"加载 {}", line);
                        AppContext.moduleLoadMap.put(line, true);
                    }

                } finally {
                    inputStream.close();
                }
            }
            //打印加载的模块信息
            ScanMetaInfoModule.printModules();
        } catch (Exception e) {
            log.error("ScanMetaInfoModule load error", e);
            System.exit(1);
        }
    }

    private static void printModules() {
        if (AppContext.moduleLoadMap.isEmpty()) {
            log.info("没有加载任何模块");
            return;
        }
        for (String module : AppContext.moduleLoadMap.keySet()) {
            log.info("加载模块- {}", module);
        }
    }
}
